package th.co.exat.uap.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import th.co.exat.uap.model.GovData;
import th.co.exat.uap.model.UAPData;
import th.co.exat.uap.util.date.FormateDate;
import th.co.exat.uap.util.exception.ErrorHandle;

public class CON004Approve extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(true);
        RequestDispatcher rd;
        List<ErrorHandle> errors = new ArrayList<ErrorHandle>();
        try {
            /** GET PARAMETER */
            String id = request.getParameter("id");
            String page = request.getParameter("p");
            String fUapDate = request.getParameter("fUapDate");
            String fToUapDate = request.getParameter("fToUapDate");
            String fPlateNo = request.getParameter("fPlate");
            String exitStationName = request.getParameter("fStation");
            String exitStation = request.getParameter("fStation2");
            String utype = request.getParameter("fUtype");
            String ustatus = request.getParameter("fUstatus");
            String pstatus = request.getParameter("fPstatus");
            /** END GET PARAMETER */

            /** UPDATE PARAMETER **/
            String plateNo = request.getParameter("plate");
            String provinceId = request.getParameter("province");
            String brandId = request.getParameter("brand");
            String colorId = request.getParameter("color");
            
            String ownerPrefix = request.getParameter("ownerPrefix");
            String ownerFname = request.getParameter("ownerFname");
            String ownerLname = request.getParameter("ownerLname");
            String ownerAddress = request.getParameter("ownerAddress");
            String ownerDistric1 = request.getParameter("ownerDistric1");
            String ownerDistric2 = request.getParameter("ownerDistric2");
            /** END UPDATE PARAMETER **/

            /** CHECK STATUS */
            if (pstatus.equals("D")) {
                //UAPData.updateStatus(id, "K");

                /** STRING ENCODING  PARAMETER */
                plateNo = new String(plateNo.getBytes("iso-8859-1"), "utf-8");
                ownerPrefix = new String(ownerPrefix.getBytes("iso-8859-1"), "utf-8");
                ownerFname = new String(ownerFname.getBytes("iso-8859-1"), "utf-8");
                ownerLname = new String(ownerLname.getBytes("iso-8859-1"), "utf-8");
                ownerAddress = new String(ownerAddress.getBytes("iso-8859-1"), "utf-8");
                ownerDistric1 = new String(ownerDistric1.getBytes("iso-8859-1"), "utf-8");
                ownerDistric2 = new String(ownerDistric2.getBytes("iso-8859-1"), "utf-8");
                /** END STRING ENCODING  PARAMETER */

                /** UPDATE DATA */
                UAPData u = new UAPData();
                u.setStatus("P1");
                u.setType("T");
                u.setOwnerPrefix(ownerPrefix);
                u.setOwnerFname(ownerFname);
                u.setOwnerLname(ownerLname);
                u.setOwnerAddress(ownerAddress);
                u.setOwnerDistric1(ownerDistric1);
                u.setOwnerDistric2(ownerDistric2);
                
                UAPData.updateData(id, u);
                /** END UPDATE DATA */

                /** GENERATE DOCUMENT NUMBER */
                int documentNo = UAPData.getDocumentNo(UAPData.getById(id).getUapDate());
                /** END GENERATE DOCUMENT NUMBER */

                /** UPDATE DOCUMENT NUMBER TO UAPData */
                UAPData.updateReportApprove(id, "P1", FormateDate.now(),
                        (String) session.getAttribute("uap_user_id"),String.valueOf(documentNo));
                /** END UPDATE DOCUMENT NUMBER TO UAPData */

                /** UPDATE GovData */
                GovData g = new GovData();
                g.setOwnerPrefix(ownerPrefix);
                g.setOwnerFname(ownerFname);
                g.setOwnerLname(ownerLname);
                g.setOwnerAddress(ownerAddress);
                g.setOwnerProvince(UAPData.getProvinceName(provinceId));
                g.setOwnerDistric1(ownerDistric1);
                g.setOwnerDistric2(ownerDistric2);
                /** HAVE DATA INOTO GovData */
                if (GovData.checkData(plateNo, UAPData.getProvinceName(provinceId)) > 0) {
                    GovData.updateData(plateNo, UAPData.getProvinceName(provinceId), g);
                } else {
                    g.setPlate1(plateNo);
                    g.setPlate2(plateNo);
                    g.setProvince(UAPData.getProvinceName(provinceId));
                    g.setBrand(UAPData.getBrandName(brandId));
                    g.setColor(UAPData.getColorName(colorId));
                    g.setPlateFull(plateNo);
                    GovData.insertData(g);
                }
                /** END UPDATE GovData */
                
            } else if (pstatus.equals("P1")) {
                /** REAPPROVE STATUS */
                UAPData.updateStatus(id, "D");
                UAPData.updateReportApprove(id, "D", "","","");
                /** END REAPPROVE STATUS */
            }
            
            /** FORWORD DATA TO JSP PAGE */
            rd = request.getRequestDispatcher("CON004Display?p=" + page
                    + "&uapDate=" + fUapDate
                    + "&toUapDate=" + fToUapDate
                    + "&plate=" + fPlateNo
                    + "&station=" + exitStationName
                    + "&station2=" + exitStation
                    + "&utype=" + utype
                    + "&ustatus=" + ustatus);
            rd.forward(request, response);
            /** END FORWORD DATA TO JSP PAGE */
        } catch (Exception ex) {
            
            ErrorHandle e = new ErrorHandle();
            e.setCode("CON004");
            e.setMessage("CON004Approve : " + ex);
            errors.add(e);

            request.setAttribute("errors", errors);

            rd = request.getRequestDispatcher("errors.jsp");
            rd.forward(request, response);
  
            //throw new ServletException(ex);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
